package com.llap.exp190929.controller;

import com.llap.exp190929.dto.PaginatedDto;
import com.llap.exp190929.entity.Category;
import com.llap.exp190929.service.CategoryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@RestController
@RequestMapping("categories")
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class CategoriesController {
    private CategoryService categoryService;

    @GetMapping("")
    public PaginatedDto<Category> findCategories(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "1000") Integer pageSize) {
        return PaginatedDto.from(categoryService.findCategories(page, pageSize));
    }

    @GetMapping("{id}")
    public Category findCategory(@PathVariable Integer id) {
        return categoryService.findCategory(id);
    }

    @PostMapping("")
    public Category addCategory(@RequestBody Category documentCategory) {
        return categoryService.addCategory(documentCategory);
    }

    @DeleteMapping("")
    public void deleteCategory(@RequestParam Integer id) {
        categoryService.deleteCategory(id);
    }

    @PatchMapping("")
    public Category updateCategory(@RequestBody Category documentCategory) {
        return categoryService.updateCategory(documentCategory);
    }
}
